﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Hfm.Common.Entity.Warehouse;
using System.Data.SqlClient;

namespace Hfm.Core.DataAccess.Warehouse
{
    public class WarehouseData
    {
        protected readonly CoreContext Context;

        public WarehouseData()
        {

        }

        public WarehouseData(CoreContext context)
        {
            Context = context;
        }

        public virtual List<NewWarehouseEntity> GetAllWarehouse(string Name, int? skip = null, int? take = null)
        {
            if ((skip == null) ^ (take == null))
                throw new ArgumentException("Skip and take parameter must be provided together or left null.");

            var query = from ss in Context.WareHousEntities
                        join s in Context.CountryEntities
                        on ss.Country equals s.CountryId into WarehouseCountry
                        from wc in WarehouseCountry.DefaultIfEmpty()
                        select new NewWarehouseEntity
                        {
                            Country =ss==null ? 0 : ss.Country,
                            CountryName=ss==null ? string.Empty: wc.CountryName,
                            PostalCode =ss==null ? string.Empty : ss.PostalCode,
                            Remarks =ss==null ? string.Empty : ss.Remarks,
                            WarehouseAddress =ss==null ? string.Empty: ss.WarehouseAddress,
                            WarehouseId =ss==null ? 0: ss.WarehouseId,
                            WarehouseName =ss==null ? string.Empty : ss.WarehouseName
                        };

            if (!string.IsNullOrWhiteSpace(Name))
            {
                query = query.Where(ss => ss.WarehouseName.Contains(Name));
            }
            query = query.OrderBy(ss => ss.WarehouseId);

            if (skip != null && take != null)
            {
                query = query.Skip(skip ?? 0).Take(take ?? int.MaxValue);
            }

            return query.ToList();
        }

        public virtual int SelectIsExites(string Name, int Id)
        {
            var entity = Context.WareHousEntities.Where(p => p.WarehouseName == Name && p.WarehouseId != Id);
            return entity.Count();
        }

        public virtual int GetWarehouseCountByName(string Name)
        {
            var query = from ss in Context.WareHousEntities select ss;

            if (!string.IsNullOrWhiteSpace(Name))
            {
                query = query.Where(ss => ss.WarehouseName.Contains(Name));
            }
            query = query.OrderBy(ss => ss.WarehouseId);

            return query.Count();
        }

        public virtual WarehousEntity GetWarehouseById(int Id)
        {
            var warehous = Context.WareHousEntities.FirstOrDefault(p => p.WarehouseId == Id);
            return warehous;
        }

        public virtual void AddWarehouse(WarehousEntity warehousentity)
        {
            Context.WareHousEntities.Add(warehousentity);
            Context.SaveChanges();
        }

        public virtual void UpdateWarehouse(WarehousEntity warehousentity)
        {
            Context.WareHousEntities.Attach(warehousentity);
            Context.Entry(warehousentity).State = System.Data.EntityState.Modified;
            Context.SaveChanges();
        }

        public virtual void DeleteWarehouse(int Id)
        {
            string strSql = "DELETE FROM Warehouse WHERE WarehouseId = @Id";
            List<SqlParameter> param = new List<SqlParameter>();
            param.Add(new SqlParameter("@Id", Id));
            Context.Database.ExecuteSqlCommand(strSql, param.ToArray());
        }
    }
}
